package com.qk.management.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;

import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 李智勇
 * @data 2025/10/5 14:47
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> listBySubject(Integer subjectId) {
        return courseMapper.selectBySubject(subjectId);
    }

    @Override
    public List<Course> list() {
        return courseMapper.selectAll();
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public void update(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public Course getById(Integer id) {
        Course course = courseMapper.selectById(id);
        return course;
    }

    @Override
    public PageResult<Course> page(Integer subject, String name, Integer target, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Course> courses = courseMapper.selectBypage(subject, name, target);
        Page<Course> pageInfo = (Page<Course>) courses;
        return PageResult.<Course>builder()
                .total(pageInfo.getTotal())
                .rows(pageInfo.getResult())
                .build();
    }

    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }
}